package com.pr.makfilm.service.impl;

import com.pr.makfilm.dao.IAbstractDao;
import com.pr.makfilm.dao.IRowHeaderDao;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.domain.RowHeader;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.service.IRowHeaderService;
import com.pr.makfilm.service.util.SidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by p.radomski on 23.01.14.
 */
@Service(RowHeaderServiceImpl.BEAN_NAME)
public class RowHeaderServiceImpl extends AbstractServiceImpl<RowHeader> implements IRowHeaderService
{
   public static final String BEAN_NAME = "rowHeaderService";

   @Resource
   private IRowHeaderDao rowHeaderDao;

   @Resource
   private IRoomService roomService;

   @Override
   protected IAbstractDao<RowHeader> getDao()
   {
      return rowHeaderDao;
   }

   @Override
   @Transactional(readOnly = true)
   public List<RowHeader> getByRoom(Room room)
   {
      return rowHeaderDao.getByRoom(room);
   }

   @Override
   @Transactional(readOnly = false)
   public RowHeader createHeader(Room room, int row, String label)
   {
      RowHeader header = new RowHeader();
      Room oldRoom = roomService.getBySid(room.getSid());
      header.setSid(SidUtils.generate());
      header.setObjectState(ObjectStateEnum.ACTIVE);
      header.setRoom(oldRoom);
      header.setLabel(label);
      header.setRow(row);
      return save(header);
   }
}
